#include ".. /source/http/http.hpp"
#include <iostream>
#include <cassert>
#include <chrono>
#include <thread>

void logMessage(const std::string& message) {
    std::cout << message << std::endl;
}

int main() {
    try {
        Socket cli_sock;
        if (!cli_sock.CreateClient(8085, "127.0.0.1")) {
            throw std::runtime_error("Failed to create client socket");
        }

        std::string req = "PUT /1234.txt HTTP/1.1\r\nConnection: keep-alive\r\n";
        std::string body;
        if (!Util::ReadFile("./hello.txt", &body)) {
            throw std::runtime_error("Failed to read file");
        }
        req += "Content-Length: " + std::to_string(body.size()) + "\r\n\r\n";

        if (cli_sock.Send(req.c_str(), req.size()) == -1) {
            throw std::runtime_error("Failed to send request header");
        }
        if (cli_sock.Send(body.c_str(), body.size()) == -1) {
            throw std::runtime_error("Failed to send request body");
        }

        char buf[1024] = {0};
        if (!cli_sock.Recv(buf, 1023)) {
            throw std::runtime_error("Failed to receive response");
        }

        logMessage("Received: " + std::string(buf));

        std::this_thread::sleep_for(std::chrono::seconds(3)); // 等待3秒
        cli_sock.Close();
    } catch (const std::exception& e) {
        logMessage("Error: " + std::string(e.what()));
        return 1;
    }

    return 0;
}
